<?php

    class TestimonialSidebarPanel extends DefaultSidebarPanel
    {
        const SIDEBAR_TEXT_LENGTH = 140;
   
        public function GetGeneratedContent( $panelInfo, TemplateEngine $tmpl, $pageId)
        {
            $testimonialsCount = Core::GetProperty("testimonials_count");
            if ( empty($testimonialsCount) )
            {
                $testimonials = Core::ResourceManager()->GetTestimonials();
                $testimonialsCount = count($testimonials);
            }
    
            $testimonialIndex = $testimonialsCount == 0 ? 0 : $this->seed % $testimonialsCount;
    
            $tmplVar = array("TESTIMONIAL_DATA" => $this->GenerateTestimonial($testimonialIndex));
            $cacheId = "html/_sidebar_panels/". $panelInfo['id'] . "_$testimonialIndex";
                
            return $this->GenerateContent($cacheId, $panelInfo, $tmpl, $tmplVar);
        }
               
        private function GenerateTestimonial( $testimonialIndex )
        {
            $content = '';
            $testimonials = Core::ResourceManager()->GetTestimonials();
    
            if ( isset($testimonials[$testimonialIndex]) )
            {
                $testimonial = $testimonials[$testimonialIndex];
                $text = $testimonial->short_text . "...";
    
                $tmpl = Core::CreateTemplateEngine();
                $tmpl->define('testimonial', 'include/testimonials/testimonial.tpl');
                $tmpl->define('photo', 'include/testimonials/testimonial_pic.tpl');
    
                $tmpl->set('NAME',      $testimonial->name);
                $tmpl->set('INFO',      $testimonial->info);
                $tmpl->set('TEXT',      $text);
    
                if ( !empty($testimonial->photo) )
                {
                    $tmpl->set('PHOTO_SRC', $testimonial->photo);
                    $tmpl->parse('PHOTO', 'photo');
                }
    
                $tmpl->parse("testimonial", "testimonial");
    
                $content = $tmpl->getGenerated();            
            }
    
            return $content;
        }
    }

?>
